package com.woniuxy.service.impl;

import com.woniuxy.entity.*;
import com.woniuxy.mapper.ImgMapper;
import com.woniuxy.mapper.ProductMapper;
import com.woniuxy.mapper.ProductOrderDetailMapper;
import com.woniuxy.mapper.ProductOrderMapper;
import com.woniuxy.service.LiuService;
import com.woniuxy.vo.ProductVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
@author Jan
@create 2020-06-02 12:33

*/
@Service
public class LiuServiceImpl implements LiuService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ImgMapper imgMapper;
    @Resource
    private ProductOrderMapper productOrderMapper;
    @Resource
    private ProductOrderDetailMapper productOrderDetailMapper;
    //查询商品集合(条件查询)
    public List<Product> selectAllProduct(Map map) {
        return productMapper.selectAllProduct(map);
    }

    //根据商品id查询所有图片,集市的typeid为5
    public List<Img> selectProductAllImg(int proId) {
        ImgExample example = new ImgExample();
        example.createCriteria().andTypeIdEqualTo(5).andOtherIdEqualTo(proId);
        List<Img> imgs = imgMapper.selectByExample(example);
        return imgs;
    }

    //上下架商品
    public int updateProductStatus(Product product) {
       return productMapper.updateByPrimaryKeySelective(product);
    }

    //新增商品返回主键
    public int insertProductReturnProId(Product product) {
        return productMapper.insertProductReturnProId(product);
    }

    //添加商品图片
    public int insertProductImg(int proId, String url) {
        Img img = new Img();
        img.setOtherId(proId);
        img.setUrl(url);
        img.setTypeId(5);
        return imgMapper.insertSelective(img);
    }

    //根据id删除图片
    public int deleteProductImg(int imgId) {
        ImgExample example = new ImgExample();
        example.createCriteria().andImgIdEqualTo(imgId);
        return imgMapper.deleteByExample(example);
    }

    //更新product
    public int updateProduct(Product product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }

    //分页查询订单
    public List<ProductOrder> selectAllProductOrder(int pageIndex, int number, int status) {
        HashMap map = new HashMap();
        map.put("pageIndex",pageIndex);
        map.put("number",number);
        map.put("status",status);
        List<ProductOrder> list = productOrderMapper.selectAllProductOrder(map);
        //继续封装下面的productOrderDetail
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                ProductOrderDetailExample example = new ProductOrderDetailExample();
                example.createCriteria().andProductOrderIdEqualTo(list.get(i).getProOrderId());
                List<ProductOrderDetail> list1 = productOrderDetailMapper.selectByExample(example);
                list.get(i).setList(list1);
            }
        }
        return list;
    }

    //查询总页数
    public int selectOrderAllCount(int status) {
        if(status==0){
            return  productOrderMapper.selectByExample(new ProductOrderExample()).size();
        }else{
            ProductOrderExample example = new ProductOrderExample();
            example.createCriteria().andStatusEqualTo(status);
            List<ProductOrder> list = productOrderMapper.selectByExample(example);
            return list.size();
        }
    }

    //确定收货,将status改为3
    public int updateProducOrderStatus(int proOrderId) {
        ProductOrder order = new ProductOrder();
        order.setProOrderId(proOrderId);
        order.setStatus(3);
        return  productOrderMapper.updateByPrimaryKeySelective(order);
    }

    //查询所有ProductVo
    public List<ProductVo> selectAllProductVo() {
        return productMapper.selectAllProductVo();
    }
}
